package mobilegeniusapp;

import java.io.IOException;
import java.util.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MobileGeniusApp extends MIDlet implements CommandListener, Runnable {

    Form menu;
    Command sair, jogar, voltar;
    StringItem mensagem;
    Display dsp;
    Displayable curr;
    Timer timer;
    NumerosCanvas canvas;
    Vector cor;
    int nivel = 0;
    int pontos = 0;
    private String keyText = "";

    public MobileGeniusApp() {
        cor  = new Vector();
        menu = new Form("Mobile Genius Java ME 1.0");
        jogar = new Command("JOGAR", Command.OK, 0);
        sair = new Command("SAIR", Command.OK, 1);

        menu.addCommand(jogar);
        menu.addCommand(sair);
        menu.setCommandListener(this);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        notifyDestroyed();
    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        dsp = Display.getDisplay(this);
        dsp.setCurrent(menu);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == jogar) {
            try {
                principal();
            } catch (InterruptedException ex) {
            }
        }

        if (c == voltar) {
            dsp.setCurrent(menu);
        }

        if (c == sair) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException e) {
            }
        }
    }

    public void principal() throws InterruptedException {
        curr = new NumerosCanvas(15);
        dsp.setCurrent(curr);

        timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                aleatorio();
            }
        }, 1000);
    }

    public void comum() {
        curr = new NumerosCanvas(15);
        dsp.setCurrent(curr);
    }

    public void aleatorio() {
        curr = new NumerosCanvas(random());
        dsp.setCurrent(curr);
    }

    private void armazena(int c) {
        cor.addElement(new Integer(c));
    }

    private int random() {
        Random sorteio = new Random();
        int k = sorteio.nextInt(9) + 1;
        armazena(k);
        return k;
    }

    public void run() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    class NumerosCanvas extends Canvas implements CommandListener, Runnable {

        int n = 0;
        int w = getWidth();
        int h = getHeight();

        public NumerosCanvas(int b) {
            n = b;
        }

        protected void paint(Graphics g) {
            if (n == 1) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/1.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 2) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/2.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 3) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/3.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 4) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/4.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 5) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/5.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 6) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/6.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 7) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/7.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 8) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/8.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 9) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/9.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 15) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/modelo_sorteio.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                } catch (IOException ex) {
                }
            }

            g.setColor(255, 255, 255);
            g.drawString(String.valueOf(cor.elementAt(nivel)) + " - " + String.valueOf(pontos) + " - " + keyText, 50, 300, Graphics.BASELINE | Graphics.HCENTER);
        }

        protected void keyPressed(int keyCode) {
            if (Integer.parseInt((String) cor.elementAt(nivel)) == Integer.parseInt(getKeyName(keyCode))) {
                pontos = pontos + 1;
            }
            keyText = getKeyName(keyCode);
            curr = new NumerosCanvas(Integer.parseInt(getKeyName(keyCode)));
            dsp.setCurrent(curr);
        }

        public void commandAction(Command c, Displayable d) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public void run() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
}